

public class MidiIn 
{
	public long lastOnset;
	public int[] pitch;
	public void newNote(int pitch, int velocity, long onset){
		if (velocity>0&&Main.score.learning)
		{
			if (Main.barClk.length>0) { // piece started
				// System.out.println("\ntime diff = "+(Main.lastClk-Main.barClk[Main.barClk.length-1]+500));
				if (Main.pchIn.length!=0&&Main.lastClk-(Main.barClk[Main.barClk.length-1]+500)>=0||(Main.lastClk-(Main.barClk[Main.barClk.length-1]+500)<0)&&System.currentTimeMillis()-(Main.barClk[Main.barClk.length-1]+500)<0)  // extend last array (still in same bar)
				{
					// System.out.println("\n1111111111111111111111");
					System.out.println("Main.pchIn.length = "+Main.pchIn.length);
					if (Main.pchIn[Main.pchIn.length-1]!=null)
					{
						Main.pchIn[Main.pchIn.length-1]=AltMath.incArray(Main.pchIn[Main.pchIn.length-1]);
						Main.pchIn[Main.pchIn.length-1][Main.pchIn[Main.pchIn.length-1].length-1]=pitch;
						Main.velIn[Main.velIn.length-1]=AltMath.incArray(Main.velIn[Main.velIn.length-1]);						
						Main.velIn[Main.velIn.length-1][Main.velIn[Main.velIn.length-1].length-1]=velocity;
						Main.rhyIn[Main.rhyIn.length-1]=AltMath.incArray(Main.rhyIn[Main.rhyIn.length-1]);												
						double bpms = (double) (Main.bpm) / 60000;
						double msPerTick = 1./(bpms*(double)(Main.stats.PPQ));
						int thisOnset = (int) ((System.currentTimeMillis()-Main.barClk[Main.barClk.length-1]-500)/msPerTick); //time / ((qpm   / tpq) / 60000) ;
						if (thisOnset>=0) {
							Main.rhyIn[Main.rhyIn.length-1][Main.rhyIn[Main.rhyIn.length-1].length-1]=thisOnset;
						}
						else {
							Main.rhyIn[Main.rhyIn.length-1][Main.rhyIn[Main.rhyIn.length-1].length-1]=thisOnset+Main.stats.TPB;
							
						}
					}
					else {
						
					}
				}
				else // add new array
				{
					// System.out.println("\n2222222222222222222222222222");
					Main.chdIn[0]=AltMath.incArray(Main.chdIn[0]);
					Main.chdIn[1]=AltMath.incArray(Main.chdIn[1]);
					Main.chdIn[2]=AltMath.incArray(Main.chdIn[2]);
					Main.chdIn[0][Main.chdIn[0].length - 1] = Main.score.changes[0][(Main.score.chordPointer)
							% Main.score.changes[0].length];
					Main.chdIn[1][Main.chdIn[1].length - 1] = Main.score.changes[1][(Main.score.chordPointer)
					                                              					% Main.score.changes[1].length];
					Main.chdIn[2][Main.chdIn[2].length - 1] = Main.score.changes[2][(Main.score.chordPointer)
					                                              					% Main.score.changes[2].length];

					int[][] tempPchIn = Main.pchIn;
					int[][] tempRhyIn = Main.rhyIn;
					int[][] tempVelIn = Main.velIn;
					Main.pchIn = new int[Main.pchIn.length+1][];
					Main.rhyIn = new int[Main.rhyIn.length+1][];
					Main.velIn = new int[Main.velIn.length+1][];
					for (int i=0;i<(Main.pchIn.length-1);i++) {
						Main.pchIn[i]=tempPchIn[i];
						Main.rhyIn[i]=tempRhyIn[i];
						Main.velIn[i]=tempVelIn[i];
					}
					Main.pchIn[Main.pchIn.length-1]=new int[1];
					Main.velIn[Main.velIn.length-1]=new int[1];
					Main.rhyIn[Main.rhyIn.length-1]=new int[1];
					
					double bpms = (double) (Main.bpm) / 60000;
					double msPerTick = 1./(bpms*(double)(Main.stats.PPQ));
					Main.rhyIn[Main.rhyIn.length-1][Main.rhyIn[Main.rhyIn.length-1].length-1]=(int) ((System.currentTimeMillis()-Main.barClk[Main.barClk.length-1]-500)/msPerTick); //time / ((qpm   / tpq) / 60000) 
				
					Main.pchIn[Main.pchIn.length-1][Main.pchIn[Main.pchIn.length-1].length-1]=pitch;
					Main.velIn[Main.velIn.length-1][Main.velIn[Main.velIn.length-1].length-1]=velocity;
				}
			}
			Main.lastClk = System.currentTimeMillis();
			 System.out.println("Main.chdIn[0].length = "+Main.chdIn[0].length);
			for (int i=0;i<Main.pchIn.length;i++) {
				System.out.print("\nBar: "+i+" Chord: "+Main.chdIn[0][i]+"\nPitch Array: ");
				for (int j=0;j<Main.pchIn[i].length;j++) {
					System.out.print("\t"+Main.pchIn[i][j]);
				}
				System.out.print("\nOnset Array: ");
				for (int j=0;j<Main.rhyIn[i].length;j++) {
					System.out.print("\t"+Main.rhyIn[i][j]);
				}
			}
		}
	}
}
